package net.huashitong.supervision.task.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.entity.StringEntity;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.dao.HastenMessageDao;
import net.huashitong.supervision.task.entity.CopeTo;
import net.huashitong.supervision.task.entity.HastenMessage;
import net.huashitong.supervision.task.entity.TaskUnit;
import net.huashitong.supervision.task.filter.HastenMessageFilter;
import net.huashitong.supervision.task.util.HolidayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/29
 * Time: 20:53
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class HastenMessageService extends StringPKService<HastenMessage> {
    @Autowired
    private HastenMessageDao hastenMessageDao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SMSUtilService smsUtilService;
    @Override
    protected BaseDao<HastenMessage, String> getDao() {
        return hastenMessageDao;
    }

    @Override
    protected void beforeSave(HastenMessage obj) {
        obj.setSysId(tableService.updateMaxSysId("t_hasten_message", null));
    }
    public void save(HastenMessage message, String userId){
        Date date = new Date();
        String date1 = HolidayUtils.sdf1.format(date);
        String time = HolidayUtils.sdf2.format(date);
        message.setSysId(tableService.updateMaxSysId("t_hasten_message",null));
        message.setCreateUser(userId);
        message.setUpdUser(userId);
        message.setCreateDate(date1);
        message.setCreateTime(time);
        message.setUpdDate(date1);
        message.setUpdTime(time);
        getDao().save(message);
    }
    public String saveHasten(HastenMessageFilter filter) {
        String createId="";
        if (StringUtils.isNotBlank(filter.getCreateId())){
            createId = filter.getCreateId();
        }else{
            createId=SysUserUtils.getUserId();
        }
        //催办的单位id
        String deptId = taskUnitService.getById(filter.getTaskUnitId()).getDeptId();
        //当前单位任务最后一级的用户信息，后期用于短信功能
        SysUser sysUser = sysUserService.getById(createId);
        List<SysUser> userList = filter.getUserList();
        TaskUnit taskUnit = taskUnitService.getById(filter.getTaskUnitId());

        List<String> userIds = new ArrayList<>();
        for (SysUser user : userList) {
            HastenMessage hastenMessage = new HastenMessage();
            BeanUtils.copyProperties(filter, hastenMessage);
            hastenMessage.setCreateUserName(sysUser.getName());
            hastenMessage.setStatus("0");//设置催办状态为未读
            hastenMessage.setDeptId(taskUnit.getDeptId());
            hastenMessage.setTaskSubId(taskUnit.getTaskSubId());
            hastenMessage.setUserId(user.getSysId());//保存接收催办信息的userid
            hastenMessage.setUserName(user.getName());
            hastenMessage.setTaskMainName(taskUnit.getTaskMainName());
            hastenMessage.setTaskSubName(taskUnit.getTaskSubName());
            this.save(hastenMessage,createId);
            //催办短信
//            SMSUtilService.taskHastenMessage(taskUnit,hastenMessage ,user.getMobile());
            userIds.add(user.getSysId());
        }
        //判断是够勾选催办发送单位领导
        if ("1".equals(filter.getType())) {
            //勾选过。创建一条给单位领导的催办信息，查询该单位的单位领导
                SysUserFilter userFilter = new SysUserFilter();
            userFilter.setDeptId(deptId);
            userFilter.setRoleId("02");
            List<String> list = sysUserService.queryLoginByRoleDept(userFilter);
            for (String s : list) {
                String name = sysUserService.getById(s).getName();
                //判断催办人员列表是否有该单位领导
                if (userIds.contains(s)){
                    //包含该领导，不重复发送
                }else{
                    //不包含。创建一条催办领导的信息
                    HastenMessage hastenMessage = new HastenMessage();
                    BeanUtils.copyProperties(filter, hastenMessage);
                    hastenMessage.setCreateUserName(sysUser.getName());
                    hastenMessage.setStatus("0");//设置催办状态为未读
                    hastenMessage.setDeptId(taskUnit.getDeptId());
                    hastenMessage.setTaskSubId(taskUnit.getTaskSubId());
                    hastenMessage.setUserId(s);//保存接收催办信息的userid
                    hastenMessage.setUserName(name);
                    hastenMessage.setTaskMainName(taskUnit.getTaskMainName());
                    hastenMessage.setTaskSubName(taskUnit.getTaskSubName());
                    String mobile = sysUserService.getById(s).getMobile();
                    //催办短信
                    smsUtilService.taskHastenMessage(taskUnit,hastenMessage ,sysUserService.getById(s));
                    this.save(hastenMessage,createId);
                }
            }
        }

        return "200";
    }

    public List<HastenMessage> queryFilter(HastenMessageFilter filter) {
        return hastenMessageDao.queryFilter(filter);
    }

    public void deleteByUnitIds(List<String> taskUnitIds) {
        hastenMessageDao.deleteByUnitIds(taskUnitIds);
    }
}
